package com.hm.pan.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.mail.internet.MimeUtility;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.hm.pan.annotation.CheckLogin;
import com.hm.pan.annotation.NoLogin;
import com.hm.pan.model.ResultObj;
import com.hm.pan.service.UserService;
import com.hm.pan.util.Generator;

import redis.clients.jedis.Jedis;

@RestController
@CheckLogin
public class Verification {
	@Value("${mail.nickname}")
	private String emailNickName;
	@Value("${mail.address}")
	private String emailAddress;
	@Autowired
	private JavaMailSender mailSender;

	@Resource
	private Jedis jedis;

	@Resource
	private UserService userService;

	// 邮箱注册验证码
	@PostMapping("verification")
	@NoLogin
	public Object testt(@RequestBody JSONObject jsonObject, HttpServletResponse response, HttpServletRequest request) {
		response.setHeader("Access-Control-Allow-Origin", "*");
		String userEmail = jsonObject.getString("userEmail");
		Map<String, Object> map = new HashMap<>();
		String a = Generator.extractCode();
		String findEmail = userService.findEmail(userEmail);
		if (findEmail == null) { // 判断邮箱号是否被已注册
			try {
				SimpleMailMessage mail = new SimpleMailMessage();
				mail.setFrom(MimeUtility.encodeText(emailNickName) + " <" + emailAddress + ">");
				mail.setTo(userEmail);
				mail.setSubject(emailNickName + "用户注册");
				mail.setText("验证码：" + a + "，感谢你注册" + emailNickName + "，请在10分钟内完成注册，请勿将你的验证码泄露给他人");
				mailSender.send(mail);
				jedis.set(userEmail, String.valueOf(a));// 将获取的验证码存进jedis里
				map.put("message", "success");
				return jsonObject.fluentPutAll(map); // 注册成功
			} catch (NullPointerException e) {
				e.printStackTrace();
			} catch (Exception e) { // 没有该邮箱号
				e.printStackTrace();
				map.put("message", "nullEmail");
				return jsonObject.fluentPutAll(map);
			}
		}
		map.put("message", "fail"); // 该邮箱号已被注册
		return jsonObject.fluentPutAll(map);
	}

	// 密码修改邮箱验证
	@PostMapping("sendCode")
	public Object sendCode(@RequestParam("userEmail") String userEmail) {
		ResultObj resultObj = new ResultObj();
		String code = Generator.extractCode(); // 获取随机验证码
		String findEmail = userService.findEmail(userEmail); // 获取数据库的邮箱
		if (findEmail != null) { // 判断邮箱是否被注册
			try {
				SimpleMailMessage mail = new SimpleMailMessage();
				mail.setFrom(MimeUtility.encodeText(emailNickName) + " <" + emailAddress + ">");
				mail.setTo(userEmail);
				mail.setSubject(emailNickName + "密码找回");
				mail.setText("你的验证码：" + code + "，感谢你使用" + emailNickName + "，请在2分钟内完成填写，请勿将你的验证码泄露给他人");
				mailSender.send(mail);
				jedis.set(userEmail, code);// 将获取的验证码存进jedis里
				resultObj.setStatusCode(200);
				resultObj.setMessage("success");
			} catch (NullPointerException e) {
				e.printStackTrace();
				resultObj.setMessage("fails");
				resultObj.setStatusCode(500);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				resultObj.setMessage("null");
			}
		}
		return resultObj;
	}

}
